package demo.qc.viewflowerguide;

import org.taptwo.android.widget.FlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 *  IfengCircleFlowIndicator
 */
public class IfengBitMapFlowIndicator extends View implements FlowIndicator {

	/**
	 * 图片的边长
	 */
	private float sideLength = 4;
	private float spacing = 4;
	private ViewFlow viewFlow;
	private int currentScroll = 0;
	private int flowWidth = 0;

	private Bitmap mBitMapInactive;
	private Bitmap mBitMapActive;
	private Paint mPaint;
	private Paint linePaint;

	/**
	 * Default constructor
	 * @param context
	 */
	public IfengBitMapFlowIndicator(Context context) {
		super(context);
		initBitMap();
	}

	/**
	 * The contructor used with an inflater
	 * 
	 * @param context
	 * @param attrs
	 */
	public IfengBitMapFlowIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		initBitMap();
	}

	private void initBitMap() {
		mBitMapInactive = BitmapFactory.decodeResource(getResources(), R.drawable.guide_point_inactive);
		mBitMapActive = BitmapFactory.decodeResource(getResources(), R.drawable.guide_point_active);
		
		sideLength = mBitMapInactive.getWidth();
		mPaint = new Paint();
		linePaint =  new Paint();
		linePaint.setStyle(Paint.Style.FILL);
		linePaint.setColor(0xFFE39393);
		linePaint.setStrokeWidth(2);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int count = 3;
		if (viewFlow != null) {
			count = viewFlow.getViewsCount();
		}
		int leftPadding = getPaddingLeft();
		
		canvas.drawLine(leftPadding+sideLength/2, sideLength/2+getPaddingTop(),getWidth()-getPaddingRight()-sideLength/2, sideLength/2+getPaddingTop(), linePaint);
		// Draw stroked 
		for (int iLoop = 0; iLoop < count; iLoop++) {
			canvas.drawBitmap(mBitMapInactive, leftPadding + (sideLength + spacing) * iLoop,
					getPaddingTop(), mPaint);
		}
		float cx = 0;
		if (flowWidth != 0) {
			cx = (currentScroll * (sideLength + spacing)) / flowWidth;
		}
		canvas.drawBitmap(mBitMapActive, leftPadding + cx, getPaddingTop(), mPaint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.taptwo.android.widget.ViewFlow.ViewSwitchListener#onSwitched(android
	 * .view.View, int)
	 */
	@Override
	public void onSwitched(View view, int position) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.taptwo.android.widget.FlowIndicator#setViewFlow(org.taptwo.android
	 * .widget.ViewFlow)
	 */
	@Override
	public void setViewFlow(ViewFlow view) {
		viewFlow = view;
		flowWidth = viewFlow.getWidth();
		invalidate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.taptwo.android.widget.FlowIndicator#onScrolled(int, int, int,
	 * int)
	 */
	@Override
	public void onScrolled(int h, int v, int oldh, int oldv) {
		setVisibility(View.VISIBLE);
		currentScroll = h;
		flowWidth = viewFlow.getWidth();
		invalidate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onMeasure(int, int)
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
	}

	/**
	 * Determines the width of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Calculate the width according the views count
		else {
			int count = 3;
			if (viewFlow != null) {
				count = viewFlow.getViewsCount();
			}
			result = (int) (getPaddingLeft() + getPaddingRight() + (count * sideLength) + (count - 1)
					* spacing + 1);
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	/**
	 * Determines the height of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Measure the height
		else {
			result = (int) (sideLength + getPaddingTop() + getPaddingBottom() + 1);
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

}